package com.dhsc.service.impl.xtqxgl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhsc.domain.ywzx.xtqxgl.BqjspzVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.xtqxgl.IBqjspzDao;
import com.dhsc.service.xtqxgl.IBqjspzService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BqjspzServiceImpl
 * @Description: 标签角色配置
 * @date: 2023/11/9 16:03
 * @author: gx
 */
@Service
@Slf4j
public class BqjspzServiceImpl implements IBqjspzService {

    @Autowired
    private IBqjspzDao bqjspzDao;

    @Override
    public LayuiTableVo queryBqjspz(BqjspzVo bqjspzVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(bqjspzVo.getPage(), bqjspzVo.getLimit());
            List<BqjspzVo> list = bqjspzDao.queryBqjspz(bqjspzVo);
            PageInfo<BqjspzVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch (Exception e){
            log.error("查询标签角色配置信息出错",e);
            throw new MyException("查询标签角色配置信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveBqjspz(BqjspzVo bqjspzVo) {
        ResponseObject obj = new ResponseObject();
        try {
            bqjspzVo.setXtlxstr(bqjspzVo.getXtlx());
            //查询角色选择中是否有相同标签名称、系统类型、已分发部门
            List<BqjspzVo> bqjspzVos = bqjspzDao.queryBqjspz(bqjspzVo);
            if (CommonUtil.isNotEmptyList(bqjspzVos)){
                obj.setStatus(202);
                obj.setMessage("角色选择中有已存在信息");
                return obj;
            }
            //将多选的角色id分隔
            String[] roleids = bqjspzVo.getRoleid().split(",");
            List<BqjspzVo> list = new ArrayList<>();
            for (int i = 0; i < roleids.length; i++) {
                BqjspzVo bqjspz = new BqjspzVo();
                bqjspz.setBqbh(bqjspzVo.getBqbh());
                bqjspz.setXtlx(bqjspzVo.getXtlx());
                bqjspz.setRoleid(roleids[i]);
                bqjspz.setCjr(bqjspzVo.getCjr());
                bqjspz.setJgdm(bqjspzVo.getJgdm());
                list.add(bqjspz);
            }
            //批量插入
            bqjspzDao.insertBqjspz(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("保存标签角色配置信息出错", e);
            throw new MyException("保存标签角色配置信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteBqjspz(BqjspzVo bqjspzVo) {
        ResponseObject obj = new ResponseObject();
        try {
            bqjspzDao.updateBqjspz(bqjspzVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除标签角色配置信息出错", e);
            throw new MyException("删除标签角色配置信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryBqjsxx() {
        ResponseObject obj = new ResponseObject();
        try {
            List<BqjspzVo> list = bqjspzDao.queryBqjsxx();
            obj.setData(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询标签角色信息出错", e);
            throw new MyException("查询标签角色信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryBqmc() {
        ResponseObject obj = new ResponseObject();
        try {
            List<BqjspzVo> list = bqjspzDao.queryBqmc();
            obj.setData(list);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("查询标签名称信息出错", e);
            throw new MyException("查询标签名称信息出错",e);
        }
        return obj;
    }
}
